// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   BalanceScrPanel.java

package com.boci.clientapp.balance;

import com.boci.clientapp.balance.action.BalanceAction;
import com.boci.clientapp.balance.vo.BalanceDetailVO;
import com.boci.clientapp.balance.vo.BalanceVO;
import com.boci.clientapp.main.ApplicationService;
import com.boci.clientapp.main.ClientAppLoader;
import com.boci.common.ui.AbstractPanel;
import com.boci.common.util.*;
import com.boci.common.vo.ResponseVO;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.*;
import javax.swing.*;

public class BalanceScrPanel extends AbstractPanel
{

    public BalanceScrPanel(Locale pLocale, String pModuleName, String pScreenName)
    {
        super(pLocale, pModuleName, pScreenName);
        msgConfigurator = ClientAppLoader.getMessagesConfigurator();
        initGUI();
        setText(pLocale);
        ccyTreeMap = new TreeMap();
        ccyList = new ArrayList();
    }

    public void setText(Locale pLocale)
    {
        super.setText(pLocale);
        currentLocale = pLocale;
        setFont(pLocale);
        if(cmbCurrency != null)
            if(lastSelectedIndex == 0)
                setConCashHoldingLabel(pLocale);
            else
            if(lastSelectedIndex > 0)
                setCashHoldingLabel(pLocale);
        btnGo.setText(msgConfigurator.getUIMessages("BALANCE.GO", pLocale));
        updateCcyComboText(pLocale);
    }

    private void setFont(Locale pLocale)
    {
        for(int i = 0; i < intLblCount; i++)
        {
            lblTexts[i].setFont(ClientAppLoader.getSmallBoldTextFont(pLocale));
            lblValues[i].setFont(ClientAppLoader.getSmallPlainTextFont(pLocale));
        }

        lblHeader.setFont(ClientAppLoader.getSmallBoldTextFont(pLocale));
        btnGo.setFont(ClientAppLoader.getPlainButtonFont(pLocale));
    }

    public void initGUI()
    {
        setBackground(Color.WHITE);
        rootPanel = new JPanel();
        headerPanel = new JPanel();
        balancePanel = new JPanel();
        setLayout(new BorderLayout());
        add(rootPanel, "Center");
        rootPanel.setLayout(new BorderLayout());
        rootPanel.add(headerPanel, "North");
        rootPanel.add(balancePanel, "Center");
        headerPanel.setPreferredSize(new Dimension(710, 25));
        headerPanel.setBackground(Color.WHITE);
        headerPanel.setLayout(null);
        cmbCurrency = new JComboBox();
        cmbCurrency.setBounds(2, 2, 120, 20);
        headerPanel.add(cmbCurrency);
        btnGo = new JButton();
        btnGo.setMargin(new Insets(0, 0, 0, 0));
        btnGo.setBounds(125, 2, 22, 20);
        headerPanel.add(btnGo);
        btnGo.addActionListener(this);
        headerPanel = new JPanel();
        headerPanel.setBounds(1, -1, 710, 25);
        headerPanel.setLayout(null);
        headerPanel.setBackground(Color.WHITE);
        balancePanel.setPreferredSize(new Dimension(710, 155));
        balancePanel.setBackground(Color.WHITE);
        balancePanel.setLayout(null);
        lblHeader = new JLabel();
        lblHeader.setOpaque(true);
        lblHeader.setBackground(new Color(192, 192, 192));
        lblHeader.setText("HKD Equivalent");
        lblHeader.setBounds(5, 0, 710, 25);
        balancePanel.add(lblHeader);
        lblTexts = new JLabel[intLblCount];
        lblValues = new JLabel[intLblCount];
        for(int i = 0; i < intLblCount; i++)
        {
            lblTexts[i] = new JLabel();
            lblTexts[i].setText((new StringBuilder()).append("LABEL ").append(i).toString());
            lblTexts[i].setBounds(10, 30 + i * 25, 400, 20);
            balancePanel.add(lblTexts[i]);
            lblValues[i] = new JLabel();
            lblValues[i].setHorizontalAlignment(11);
            lblValues[i].setText("0");
            lblValues[i].setBounds(512, 27 + i * 25, 200, 20);
            balancePanel.add(lblValues[i]);
        }

    }

    public void actionPerformed(ActionEvent e)
    {
        PerformanceLogger.startAppTimer();
        String strActionType = e.getActionCommand();
        lastSelectedIndex = cmbCurrency.getSelectedIndex();
        if(cmbCurrency.getSelectedIndex() == 0)
        {
            strActionType = "GO";
            BalanceAction action = new BalanceAction();
            action.doProcess(getInputValue(strActionType), strActionType);
        } else
        {
            strActionType = "GO";
            BalanceAction action = new BalanceAction();
            action.doProcess(getInputValue(strActionType), strActionType);
        }
        PerformanceLogger.stopAppTimer();
        PerformanceLogger.doLog((new StringBuilder()).append(getScreenName()).append(".").append(strActionType).toString());
    }

    public void refresh()
    {
        if(cmbCurrency != null && cmbCurrency.getItemCount() > 0)
            cmbCurrency.setSelectedIndex(0);
        String strActionType = "GO";
        BalanceAction action = new BalanceAction();
        action.doProcess(getInputValue(strActionType), strActionType);
    }

    public void refresh(ResponseVO pResponseVO)
    {
        if(pResponseVO == null)
        {
            balanceVO = null;
            this.balanceDetailVO = null;
            clearLabelText();
            if(cmbCurrency.getSelectedIndex() == 0)
                lblHeader.setText(msgConfigurator.getUIMessages("BALANCE.HKD_EQUIVALENT", currentLocale));
            else
                lblHeader.setText((new StringBuilder()).append(msgConfigurator.getUIMessages("BALANCE.BALANCE", currentLocale)).append(" (").append((String)cmbCurrency.getSelectedItem()).append(")").toString());
            return;
        }
        balanceVO = (BalanceVO)pResponseVO;
        ccyTreeMap = new TreeMap();
        ArrayList voList = ((BalanceVO)pResponseVO).getBalanceDetailVOList();
        ArrayList keys = new ArrayList();
        ArrayList values = new ArrayList();
        keys.add("HKD");
        values.add(msgConfigurator.getUIMessages("BALANCE.HKD_EQUIVALENT", currentLocale));
        for(int i = 0; i < voList.size(); i++)
        {
            BalanceDetailVO balanceDetailVO = (BalanceDetailVO)voList.get(i);
            keys.add(balanceDetailVO.getCurrency());
            values.add(msgConfigurator.getUIMessages(balanceDetailVO.getCurrency(), currentLocale));
        }

        ccyList = keys;
        Object dstObjs[] = values.toArray();
        boolean bIsSetLastSelectedIndex = false;
        if(cmbCurrency.getSelectedIndex() == lastSelectedIndex)
            bIsSetLastSelectedIndex = true;
        cmbCurrency.setModel(new DefaultComboBoxModel(dstObjs));
        if(bIsSetLastSelectedIndex)
            cmbCurrency.setSelectedIndex(lastSelectedIndex);
        int intSelectedIndex = cmbCurrency.getSelectedIndex();
        if(intSelectedIndex == 0)
        {
            this.balanceDetailVO = getVOByCcy((String)ccyList.get(intSelectedIndex), balanceVO.getConBalanceDetailVOList());
            setConCashHoldingLabel(currentLocale);
        } else
        {
            this.balanceDetailVO = getVOByCcy((String)ccyList.get(intSelectedIndex), balanceVO.getBalanceDetailVOList());
            setCashHoldingLabel(currentLocale);
        }
    }

    public HashMap getInputValue(String pActionType)
    {
        HashMap inpMap = new HashMap();
        String strAcctNo = ApplicationService.getInstance().getAccountNo();
        String strCcy = "ALL";
        inpMap.put("selectAcct", strAcctNo);
        int intSelectedIndex = cmbCurrency.getSelectedIndex();
        if(intSelectedIndex > 0)
            strCcy = (String)ccyList.get(intSelectedIndex);
        inpMap.put("selectCurrency", strCcy);
        return inpMap;
    }

    public boolean validate(String pActionType)
    {
        return true;
    }

    public void updateCcyComboText(Locale pLocale)
    {
        if(ccyList == null)
            return;
        ArrayList values = new ArrayList();
        for(int i = 0; i < ccyList.size(); i++)
            if(i == 0)
                values.add(msgConfigurator.getUIMessages("BALANCE.HKD_EQUIVALENT", pLocale));
            else
                values.add(msgConfigurator.getUIMessages((String)ccyList.get(i), pLocale));

        Object dstObjs[] = values.toArray();
        cmbCurrency.setModel(new DefaultComboBoxModel(dstObjs));
        cmbCurrency.setFont(ClientAppLoader.getSmallPlainTextFont(pLocale));
        if(cmbCurrency.getItemCount() > 0)
            cmbCurrency.setSelectedIndex(lastSelectedIndex);
        cmbCurrency.requestFocus();
    }

    public BalanceDetailVO getVOByCcy(String pKey, ArrayList pList)
    {
        if(pList == null || pList.isEmpty())
            return null;
        for(int i = 0; i < pList.size(); i++)
        {
            BalanceDetailVO balanceDetailVO = (BalanceDetailVO)pList.get(i);
            if(balanceDetailVO.getCurrency().equals(pKey))
                return balanceDetailVO;
        }

        return null;
    }

    public String getCcyKeyFromTreeMap(String pValue, TreeMap pTreeMap)
    {
        for(Iterator it = pTreeMap.keySet().iterator(); it.hasNext();)
        {
            String strKey = (String)it.next();
            String strValue = (String)pTreeMap.get(strKey);
            if(strValue != null && strValue.equals(pValue))
                return strKey;
        }

        return null;
    }

    public ArrayList getCcyListFromArray(Object pObjs[], TreeMap pTreeMap)
    {
        if(pObjs == null || pObjs.length == 0)
            return null;
        ArrayList rtnList = new ArrayList();
        rtnList.add("HKD");
        for(int i = 1; i < pObjs.length; i++)
        {
            String strTmpValue = (String)pObjs[i];
            String strTmpKey = getCcyKeyFromTreeMap(strTmpValue, pTreeMap);
            rtnList.add(strTmpKey);
        }

        return rtnList;
    }

    public void clearLabelText()
    {
        for(int i = 0; i < intLblCount; i++)
        {
            lblTexts[i].setVisible(false);
            lblTexts[i].setText("");
            lblValues[i].setVisible(false);
            lblValues[i].setText("");
        }

    }

    public void setConCashHoldingLabel(Locale pLocale)
    {
        clearLabelText();
        if(balanceDetailVO == null)
            return;
        String ccy = balanceDetailVO.getCurrency();
        ccy = msgConfigurator.getUIMessages(ccy, pLocale);
        int intLblIndex = 0;
        lblHeader.setText(msgConfigurator.getUIMessages("BALANCE.HKD_EQUIVALENT", pLocale));
        String strTmp = StringUtil.formatPriceValue(balanceDetailVO.getTradeDateBalance(), ccy);
        if(strTmp != null)
        {
            lblTexts[intLblIndex].setText(msgConfigurator.getUIMessages("BALANCE.CON_TRA_DATE", pLocale));
            lblTexts[intLblIndex].setVisible(true);
            lblValues[intLblIndex].setText(strTmp);
            lblValues[intLblIndex].setVisible(true);
            intLblIndex++;
        }
        strTmp = StringUtil.formatPriceValue(balanceDetailVO.getSettlementDateBalance(), ccy);
        if(strTmp != null)
        {
            lblTexts[intLblIndex].setText(msgConfigurator.getUIMessages("BALANCE.CON_SET_DATE", pLocale));
            lblTexts[intLblIndex].setVisible(true);
            lblValues[intLblIndex].setText(strTmp);
            lblValues[intLblIndex].setVisible(true);
            intLblIndex++;
        }
        strTmp = StringUtil.formatPriceValue(balanceDetailVO.getMarketValue(), ccy);
        if(strTmp != null)
        {
            lblTexts[intLblIndex].setText(msgConfigurator.getUIMessages("BALANCE.CON_MAR_VALUE", pLocale));
            lblTexts[intLblIndex].setVisible(true);
            lblValues[intLblIndex].setText(strTmp);
            lblValues[intLblIndex].setVisible(true);
            intLblIndex++;
        }
        strTmp = StringUtil.formatPriceValue(balanceDetailVO.getPortfolioValue(), ccy);
        if(strTmp != null)
        {
            lblTexts[intLblIndex].setText(msgConfigurator.getUIMessages("BALANCE.CON_POR_VALUE", pLocale));
            lblTexts[intLblIndex].setVisible(true);
            lblValues[intLblIndex].setText(strTmp);
            lblValues[intLblIndex].setVisible(true);
            intLblIndex++;
        }
        strTmp = StringUtil.formatPriceValue(balanceDetailVO.getBuyingPower(), ccy);
        if(strTmp != null)
        {
            lblTexts[intLblIndex].setText(msgConfigurator.getUIMessages("BALANCE.CON_BUY_POWER_FOR_QUA_SECURITIES", pLocale));
            lblTexts[intLblIndex].setVisible(true);
            lblValues[intLblIndex].setText(strTmp);
            lblValues[intLblIndex].setVisible(true);
            intLblIndex++;
        }
        strTmp = StringUtil.formatPriceValue(balanceDetailVO.getInvestmentPower(), ccy);
        if(strTmp != null)
        {
            lblTexts[intLblIndex].setText(msgConfigurator.getUIMessages("BALANCE.CON_INV_POW_HKD", pLocale));
            lblTexts[intLblIndex].setVisible(true);
            lblValues[intLblIndex].setText(strTmp);
            lblValues[intLblIndex].setVisible(true);
            intLblIndex++;
        }
    }

    public void setCashHoldingLabel(Locale pLocale)
    {
        clearLabelText();
        if(balanceDetailVO == null)
            return;
        String ccy = balanceDetailVO.getCurrency();
        ccy = msgConfigurator.getUIMessages(ccy, pLocale);
        int intLblIndex = 0;
        lblHeader.setText((new StringBuilder()).append(msgConfigurator.getUIMessages("BALANCE.BALANCE", pLocale)).append(" (").append(ccy).append(")").toString());
        String strTmp = StringUtil.formatPriceValue(balanceDetailVO.getTradeDateBalance(), ccy);
        if(strTmp != null)
        {
            lblTexts[intLblIndex].setText(msgConfigurator.getUIMessages("BALANCE.TRA_DATE", pLocale));
            lblTexts[intLblIndex].setVisible(true);
            lblValues[intLblIndex].setText(strTmp);
            lblValues[intLblIndex].setVisible(true);
            intLblIndex++;
        }
        strTmp = StringUtil.formatPriceValue(balanceDetailVO.getSettlementDateBalance(), ccy);
        if(strTmp != null)
        {
            lblTexts[intLblIndex].setText(msgConfigurator.getUIMessages("BALANCE.SET_DATE", pLocale));
            lblTexts[intLblIndex].setVisible(true);
            lblValues[intLblIndex].setText(strTmp);
            lblValues[intLblIndex].setVisible(true);
            intLblIndex++;
        }
        strTmp = StringUtil.formatPriceValue(balanceDetailVO.getMarketValue(), ccy);
        if(strTmp != null)
        {
            lblTexts[intLblIndex].setText(msgConfigurator.getUIMessages("BALANCE.MAR_VALUE", pLocale));
            lblTexts[intLblIndex].setVisible(true);
            lblValues[intLblIndex].setText(strTmp);
            lblValues[intLblIndex].setVisible(true);
            intLblIndex++;
        }
        strTmp = StringUtil.formatPriceValue(balanceDetailVO.getPortfolioValue(), ccy);
        if(strTmp != null)
        {
            lblTexts[intLblIndex].setText(msgConfigurator.getUIMessages("BALANCE.POR_VALUE", pLocale));
            lblTexts[intLblIndex].setVisible(true);
            lblValues[intLblIndex].setText(strTmp);
            lblValues[intLblIndex].setVisible(true);
            intLblIndex++;
        }
    }

    public void setDefFocus()
    {
        SwingUtilities.invokeLater(new Runnable() {

            public void run()
            {
                cmbCurrency.requestFocus();
            }
        }
);
    }

    private final int intLblCount = 6;
    private JLabel lblValues[];
    private JButton btnGo;
    private JComboBox cmbCurrency;
    private JPanel rootPanel;
    private JPanel headerPanel;
    private JPanel balancePanel;
    private JLabel lblHeader;
    private JLabel lblTexts[];
    private Locale currentLocale;
    private TreeMap ccyTreeMap;
    private ArrayList ccyList;
    private BalanceVO balanceVO;
    private BalanceDetailVO balanceDetailVO;
    private int lastSelectedIndex;
    MessagesConfigurator msgConfigurator;

}
